/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.index.MessageHit;
/*     */ import com.zimbra.cs.index.SearchParams;
/*     */ import com.zimbra.cs.index.SearchParams.Fetch;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.index.ZimbraHit;
/*     */ import com.zimbra.cs.index.ZimbraQueryResults;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mime.MPartInfo;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SearchWrapper
/*     */   extends PhantomResource
/*     */ {
/*     */   protected static final long DAY_IN_MS = 86400000L;
/*     */   protected static final long WEEK_IN_MS = 604800000L;
/*     */   protected static final long MONTH_IN_MS = 2419200000L;
/*     */   protected static final int SEARCH_LIMIT = 100;
/*     */   private String mContentType;
/*     */   private StringBuilder mQuery;
/*     */   
/*     */   public SearchWrapper(String uri, String owner)
/*     */   {
/*  61 */     this(uri, owner, parseUri(uri));
/*     */   }
/*     */   
/*     */   public SearchWrapper(String uri, String owner, List<String> tokens) {
/*  65 */     super(uri, owner, tokens);
/*  66 */     this.mContentType = null;
/*  67 */     this.mQuery = new StringBuilder();
/*  68 */     this.mQuery.append("has:attachment ");
/*  69 */     String prevToken = null;
/*  70 */     for (String token : tokens) {
/*  71 */       buildQuery(prevToken, token);
/*  72 */       prevToken = token;
/*     */     }
/*     */   }
/*     */   
/*     */   private void buildQuery(String prevTerm, String term) {
/*  77 */     if (term.equals("today")) {
/*  78 */       this.mQuery.append("after:\"-1day\" ");
/*  79 */     } else if (term.equals("last-week")) {
/*  80 */       this.mQuery.append("after:\"-1week\" ");
/*  81 */     } else if (term.equals("last-month")) {
/*  82 */       this.mQuery.append("after:\"-1month\" ");
/*  83 */     } else if (term.equals("last-year")) {
/*  84 */       this.mQuery.append("after:\"-1year\" ");
/*  85 */     } else if ("by-sender".equals(prevTerm)) {
/*  86 */       this.mQuery.append("from:(@" + term + ") ");
/*  87 */     } else if ("by-type".equals(prevTerm)) {
/*  88 */       this.mQuery.append("attachment:\"" + term + "\" ");
/*  89 */       if (!term.equals("any")) {
/*  90 */         this.mContentType = getActualContentType(term);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*  97 */   private static HashMap<String, String> sCTMap = new HashMap();
/*  98 */   static { sCTMap.put("ppt", "application/vnd.ms-powerpoint");
/*  99 */     sCTMap.put("excel", "application/vnd.ms-excel");
/* 100 */     sCTMap.put("word", "application/msword");
/* 101 */     sCTMap.put("pdf", "application/pdf");
/*     */   }
/*     */   
/*     */   private static String getActualContentType(String str) {
/* 105 */     String v = (String)sCTMap.get(str);
/* 106 */     if (v != null) {
/* 107 */       return v;
/*     */     }
/* 109 */     return str;
/*     */   }
/*     */   
/*     */   public boolean isCollection()
/*     */   {
/* 114 */     return true;
/*     */   }
/*     */   
/*     */   public Collection<DavResource> getChildren(DavContext ctxt)
/*     */   {
/* 119 */     ArrayList<DavResource> children = new ArrayList();
/* 120 */     String user = ctxt.getUser();
/* 121 */     Provisioning prov = Provisioning.getInstance();
/* 122 */     ZimbraQueryResults zqr = null;
/*     */     try {
/* 124 */       Account account = prov.get(Key.AccountBy.name, user);
/* 125 */       Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 126 */       SearchParams params = new SearchParams();
/* 127 */       params.setQueryString(this.mQuery.toString());
/* 128 */       params.setTypes(SEARCH_TYPES);
/* 129 */       params.setSortBy(SortBy.NAME_ASC);
/* 130 */       params.setFetchMode(SearchParams.Fetch.NORMAL);
/* 131 */       params.setPrefetch(true);
/* 132 */       params.setChunkSize(100);
/* 133 */       zqr = mbox.index.search(SoapProtocol.Soap12, ctxt.getOperationContext(), params);
/* 134 */       while (zqr.hasNext()) {
/* 135 */         ZimbraHit hit = zqr.getNext();
/* 136 */         if ((hit instanceof MessageHit))
/* 137 */           addAttachmentResources((MessageHit)hit, children);
/*     */       }
/*     */     } catch (Exception e) {
/* 140 */       ZimbraLog.dav.error("can't search: uri=" + getUri(), e);
/*     */     } finally {
/* 142 */       Closeables.closeQuietly(zqr);
/*     */     }
/* 144 */     return children;
/*     */   }
/*     */   
/*     */   private void addAttachmentResources(MessageHit hit, List<DavResource> children) {
/*     */     try {
/* 149 */       msg = hit.getMessage();
/* 150 */       List<MPartInfo> parts = Mime.getParts(msg.getMimeMessage());
/* 151 */       for (MPartInfo p : parts) {
/* 152 */         String name = p.getFilename();
/* 153 */         String ct = p.getContentType();
/* 154 */         if ((this.mContentType == null) || (ct == null) || (ct.startsWith(this.mContentType)))
/*     */         {
/* 156 */           if ((name != null) && (name.length() > 0))
/* 157 */             children.add(new Attachment(getUri() + name, getOwner(), msg.getDate(), p.getSize())); }
/*     */       }
/*     */     } catch (Exception e) { Message msg;
/* 160 */       ZimbraLog.dav.error("can't get attachments from msg: itemid:" + hit.getItemId(), e);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/SearchWrapper.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */